import { IDownloadFileParams, IDownloadFileRes } from "../types/file.ts";
import { ipcRenderer } from "electron";

export const FilePreload = {
  getDefaultDownLoadPath: () => {
    return ipcRenderer.invoke("getDefaultDownLoadPath");
  },
  // 打开文件选择
  openFileByPath: (filePath: string) => {
    return ipcRenderer.send("openFileByPath", filePath);
  },
  // 另存为
  saveFileAs: (sourceFilePath: string) => {
    return ipcRenderer.invoke("saveFileAs", sourceFilePath);
  },

  // 复制文件
  copyFileByPath: (filePath: string) => {
    return ipcRenderer.invoke("copyFileByPath", filePath);
  },
  // 打开文件所在目录
  openFileDirectoryByPath: (filePath: string) => {
    return ipcRenderer.invoke("openFileDirectoryByPath", filePath);
  },
  // 下载文件
  downloadFile: (params: IDownloadFileParams) => {
    ipcRenderer.send("downloadFile", params);
  },
  // 下载进度
  downloadProgress: (callBack: Function) => {
    ipcRenderer.on("downloadProgress", (_event: any, data: IDownloadFileRes) => callBack(data));
  },
  // 下载完成
  downloadDone: (callBack: Function) => {
    ipcRenderer.on("downloadDone", (_event: any, data: any) => callBack(data));
  },
  // 暂停下载
  pauseDownload: (taskId: string) => {
    ipcRenderer.send("pauseDownload", taskId);
  },
  // 恢复下载
  resumeDownload: (taskId: string) => {
    ipcRenderer.send("resumeDownload", taskId);
  },
  // 取消下载
  cancelDownload: (taskId: string) => {
    ipcRenderer.send("cancelDownload", taskId);
  },
};
